
import { ref, onMounted } from 'vue'
import type { Ref } from 'vue'
import { messageListApi, messageListApis } from '@/services/xiaoxi'

//返回值多为数组
//在ts的环境中，一定要指定类型为tuple
export const useDialogVisible = (visible = false): [Ref<boolean>, (vis: boolean) => void] => {
    const data = ref(false)
    const setData = (vis: boolean) => {
        data.value = vis
    }
    return [data, setData]
}


//用户类别数据
export const useUserList = (pageNum = 1,query={}): [Ref<Imessage.Xiaoxi[]>, Ref<number>, (page?: number,query?:object) => Promise<void>] => {
    const data = ref<Imessage.Xiaoxi[]>([])
    const total = ref<Imessage.ImessageList['total']>(0)


    const loadData = async (page = 1,query={}) => {
        let ret = await messageListApi(page,query)
        // console.log(ret.data);
        if (ret.code === 200) {
            data.value = ret.data.xiaoxi
            total.value = ret.data.total
        }
    }
    onMounted(() => {
        loadData(pageNum,query)
    })
    return [data, total, loadData]
}


export const useUserLists = (pageNum = 1): [Ref<Imessage.Xiaoxi[]>, Ref<number>, (page?: number) => Promise<void>] => {
    const data = ref<Imessage.Xiaoxi[]>([])
    const total = ref<Imessage.ImessageList['total']>(0)
    const loadData = async (page = 1) => {
        let ret = await messageListApis(page)
        // console.log(ret.data);
        if (ret.code === 200) {
            data.value = ret.data.xiaoxi
            total.value = ret.data.total
        }
    }
    onMounted(() => {
        loadData(pageNum)
    })
    return [data, total, loadData]
}



// export const useUserListss = (pageNum = 1, query = {}):
//     [Ref<UserModule.IUserInfo[]>, Ref<number>, (page?: number, query?: object) => Promise<void>] => {

//     const data = ref<UserModule.IUserInfo[]>([])
//     const total = ref(0)
//     const loadData = async (page = 1, query = {}) => {
//         let ret = await userListApi(page, query)
//         if (ret.code === 0) {
//             data.value = ret.data.users
//             total.value = ret.data.total
//         }
//     }

//     onMounted(() => {
//         loadData(pageNum, query)
//     })

//     return [data, total, loadData]
// }